Python tutorial

Launch this notebook with:

  1. Open terminal
  2. type cd training/python
  3. type ipython notebook

basics


In [1]:
1 + 1


Out[1]:
2

In [2]:
12 * 44


Out[2]:
528

In [3]:
Hello Data Skills!


  File "<ipython-input-3-ffc4546b2fc9>", line 1
    Hello Data Skills!
             ^
SyntaxError: invalid syntax

In [4]:
'Hello Data Skills!'


Out[4]:
'Hello Data Skills!'

In [6]:
print 'Hello Data Skills!'


Hello Data Skills!

In [8]:
print "Hello Data Skills!"
print 'Hello Data Skills!'
print """Hello Data Skills!"""


Hello Data Skills!
Hello Data Skills!
Hello Data Skills!

types


In [10]:
type(1)


Out[10]:
int

In [11]:
type(1 + 1)


Out[11]:
int

In [21]:
type("hello")


Out[21]:
str

In [13]:
1 == 2


Out[13]:
False

In [14]:
1 == 1


Out[14]:
True

In [15]:
type(1 == 1)


Out[15]:
bool

In [16]:
True


Out[16]:
True

In [17]:
False


Out[17]:
False

In [18]:
type(True)


Out[18]:
bool

In [20]:
12 + 12 + "hello"


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-756092b166ff> in <module>()
----> 1 12 + 12 + "hello"

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [22]:
str(1)


Out[22]:
'1'

In [23]:
type(str(1))


Out[23]:
str

In [42]:
type(3.14)


Out[42]:
float

In [71]:
3 / 2


Out[71]:
1

In [72]:
3 / 2.0


Out[72]:
1.5

variables


In [25]:
x = 1

In [26]:
print x


1

In [27]:
y = x + 2

In [28]:
print y


2

In [29]:
z = "hello"

In [30]:
z


Out[30]:
'hello'

In [31]:
w = "python"

In [32]:
print z + w


hellopython

In [33]:
print z + " " + w


hello python

In [34]:
x


Out[34]:
1

In [35]:
x = x + 1

In [36]:
print x


2

In [41]:
type(x)


Out[41]:
int

control statements


In [44]:
i = 10
print i % 2


0

In [45]:
i = 9
print i % 2


1

In [119]:
if i % 2 == 0:
    print "even"


even

In [48]:
if i % 2 == 0:
    print "even"
else:
    print "odd"


odd

In [49]:
i = 10

In [51]:
# execute "if" the code above with the new value

Excercise: Write a statement which say "it's small" for v < 10 and it says "it's big" for v >= 10


In [87]:
v = 1

In [88]:
if v < 10:
    print "it's small"
else:
    print "it's big"


it's small

Functions


In [52]:
def printeven(num):
    if num % 2 == 0:
        print "even"
    else:
        print "false"

In [53]:
printeven(10)


even

In [54]:
printeven(9)


false

In [56]:
printeven(i)


even

In [68]:
def printhuf(usd):
    print "$" + str(usd) + " is " + str(272.9 * usd) + " Ft"

In [69]:
printhuf(10)


$10 is 2729.0 Ft

Exercise: Write a function that converts hufs to dollars and prints them


In [75]:
def printusd(huf):
    print str(huf) + " Ft" + " is $" + str(huf / 272.9)

In [76]:
printusd(1000)


1000 Ft is $3.66434591425

In [96]:
def printconvert(amount, currency):
    if currency == "usd":
        printhuf(amount)
    else:
        printusd(amount)

In [93]:
printconvert(10, "usd")


$10 is 2729.0 Ft

return


In [80]:
def tohuf(usd):
    return 272.9 * usd

In [81]:
tohuf(10)


Out[81]:
2729.0

In [153]:
x = printhuf(10)


$10 is 2729.0 Ft

In [154]:
print x


None

In [155]:
x is None


Out[155]:
True

In [156]:
isnone = x is None

In [157]:
print isnone


True

In [84]:
y = tohuf(10)

In [85]:
print y


2729.0

exercise

  1. create the tousd() function
  2. Create a function that takes an amount and "what" and returns the converted value.

In [99]:
# solution
def tousd(huf):
    return huf / 272.9

def convert(amount, currency):
    if currency == "usd":
        return tohuf(amount)
    else:
        return tousd(amount)

print convert(10,"usd")


2729.0

lists


In [104]:
l = [2,4,6]

In [121]:
type(l)


Out[121]:
list

In [101]:
print l[0]
print l[2]


2
6

In [102]:
print l[3]


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-102-e30cce092d00> in <module>()
----> 1 print l[3]

IndexError: list index out of range

In [105]:
l.append(8)

In [106]:
print l


[2, 4, 6, 8]

In [107]:
l[3]


Out[107]:
8

In [108]:
for elem in l:
    print elem


2
4
6
8

In [112]:
range(1,10)


Out[112]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

In [113]:
range(1,11)


Out[113]:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [114]:
tenelements = range(1,11)

In [115]:
for e in tenelements:
    print e


1
2
3
4
5
6
7
8
9
10

In [118]:
for e in tenelements:
    print tohuf(e)


272.9
545.8
818.7
1091.6
1364.5
1637.4
1910.3
2183.2
2456.1
2729.0

exercise:

  1. create a loop which goes from 1 to 10 but only print the even numbers
  2. transform this loop so it uses an "even" function which returns if a number is even or not (bool)

In [122]:
# solution
for e in range(1,11):
    if e % 2 == 0:
        print e


2
4
6
8
10

In [123]:
def even(num):
    return num % 2 == 0

In [124]:
for e in range(1,11):
    if even(e):
        print e


2
4
6
8
10

dictionaries


In [127]:
u = {
    "username": "lili",
    "password": "1982"
}

In [128]:
u["username"]


Out[128]:
'lili'

In [129]:
u["password"]


Out[129]:
'1982'

In [130]:
print u


{'username': 'lili', 'password': '1982'}

In [137]:
import json

In [142]:
with open("passwords.json") as f:
    d = json.load(f)

In [143]:
d


Out[143]:
[{u'password': u'1982', u'username': u'lili'},
 {u'password': u'skateordie', u'username': u'cucu'},
 {u'password': u'imserious', u'username': u'maxwell'}]

In [145]:
type(d)


Out[145]:
list

In [146]:
d[2]


Out[146]:
{u'password': u'imserious', u'username': u'maxwell'}

In [148]:
for u in d:
    print u


{u'username': u'lili', u'password': u'1982'}
{u'username': u'cucu', u'password': u'skateordie'}
{u'username': u'maxwell', u'password': u'imserious'}

In [149]:
for u in d:
    print u["username"]


lili
cucu
maxwell

Exercise:

create a function that gets a username as an argument and prints "user found!!" that user is in the json file or not


In [161]:
# Solution
def printifinfile(username):
    with open("passwords.json") as f:
        j = json.load(f)
    for record in j:
        if record["username"] == username:
            print "user found!!"

In [162]:
printifinfile("lili")


user found!!

In [163]:
printifinfile("newuser")

In [ ]: